﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_GUI.Utils;
using ANZ_AIM_DAL;
using ANZ_AIM_BLL;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_FRAMEWORK;
using log4net;

namespace ANZ_AIM_GUI
{
    public partial class frmCandidateUpdateInfo : Form
    {
        //log4net
        private static readonly ILog logger = LogManager.GetLogger(typeof(frmCandidateUpdateInfo));

        public Candidate candidate { get; set; }
        public frmCandidateUpdateInfo()
        {
            InitializeComponent();
        }
        public frmCandidateUpdateInfo(string CVCode)
            : this()
        {
            try
            {
                LoadRole();
                LoadLevel();
                LoadStatus();

                CandidateBLL candidateBll = new CandidateBLL();
                candidate = candidateBll.GetById(CVCode);
                UnPackObjectData_Candidate();
            }
            catch (Exception ex) { throw ex; }
        }

        public void UnPackObjectData_Candidate()
        {
            try
            {
                txtPersonalCode.Text = candidate.CVCode;
                txtFullNameE.Text = candidate.FullName_E;
                txtFullNameV.Text = candidate.FullName_V;
                cbRole.Text = candidate.Role;
                cbLevel.Text = candidate.Level;
                txtCategory.Text = candidate.Category;
                txtProductCategory.Text = candidate.ProductCategory;
                txtProduct.Text = candidate.Product;
                txtLeaderCode.Text = candidate.TeamLeaderCode;
                dtJoiningDate.Value = Convert.ToDateTime(candidate.JoiningDate != null ? candidate.JoiningDate : (DateTime?)null);
                dtTransferDate.Value = Convert.ToDateTime(candidate.PromotionTransferDate != null ? candidate.PromotionTransferDate : (DateTime?)null);
                cbStatus.Text = candidate.Status;

                txtFingerID.Text = candidate.FingerID;
                txtPersonalTaxCode.Text = candidate.PersonalTaxCode;

                txtContactType.Text = candidate.ContractType;
                txtContactTerm.Text = candidate.ContractTerm;
                txtContractorName.Text = candidate.ContractorName;
                dtContractExpiryDate.Value = Convert.ToDateTime(candidate.ContractExpiryDate != null ? candidate.ContractExpiryDate : (DateTime?)null);
                dtContractSignDate.Value = Convert.ToDateTime(candidate.ContractSignDate != null ? candidate.ContractSignDate : (DateTime?)null);

                txtBankAccount.Text = candidate.BankAccount;
                txtBankAddress.Text = candidate.BankBranchAddress;
                txtBankName.Text = candidate.BankName;

                txtFillingRefCode.Text = candidate.FillingRefCode;
                txtProgressTarget.Text = candidate.ProfressiveTarget;
            }
            catch (Exception ex) { throw ex; }
        }

        public void PackObjectData_Candidate()
        {
            try
            {
                candidate.Role = cbRole.Text;
                candidate.Level = cbLevel.Text;
                candidate.Category = txtCategory.Text;
                candidate.ProductCategory = txtProductCategory.Text;
                candidate.Product = txtProduct.Text;
                candidate.TeamLeaderCode = txtLeaderCode.Text;
                candidate.JoiningDate = MiscHelper.DateTimeToShort(dtJoiningDate.Value);
                candidate.PromotionTransferDate = MiscHelper.DateTimeToShort(dtTransferDate.Value);
                candidate.Status = cbStatus.Text;

                candidate.FingerID = txtFingerID.Text;
                candidate.PersonalTaxCode = txtPersonalTaxCode.Text;

                candidate.ContractTerm = txtContactTerm.Text;
                candidate.ContractType = txtContactType.Text;
                candidate.ContractorName = txtContractorName.Text;
                candidate.ContractSignDate = MiscHelper.DateTimeToShort(dtContractSignDate.Value);
                candidate.ContractExpiryDate = MiscHelper.DateTimeToShort(dtContractExpiryDate.Value);

                candidate.BankAccount = txtBankAccount.Text;
                candidate.BankBranchAddress = txtBankAddress.Text;
                candidate.BankName = txtBankName.Text;

                candidate.FillingRefCode = txtFillingRefCode.Text;
                candidate.ProfressiveTarget = txtProgressTarget.Text;
            }
            catch (Exception ex) { throw ex; }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                cbRole.SelectedIndex = 0;
                cbLevel.SelectedIndex = 0;
                cbStatus.SelectedIndex = 0;
                txtProductCategory.Text = "";
                txtProduct.Text = "";
                txtLeaderCode.Text = "";
                dtJoiningDate.IsEmpty = true;
                dtTransferDate.IsEmpty = true;
                txtCategory.Text = "";
                txtFingerID.Text = "";
                txtPersonalTaxCode.Text = "";
                txtContactType.Text = "";
                txtContractorName.Text = "";
                txtContactTerm.Text = "";
                dtContractExpiryDate.IsEmpty = true;
                dtContractSignDate.IsEmpty = true;
                txtBankAccount.Text = "";
                txtBankName.Text = "";
                txtBankAddress.Text = "";
                txtFillingRefCode.Text = "";
                txtProgressTarget.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (validateForm())
            {
                try
                {
                    this.PackObjectData_Candidate();
                    CandidateBLL candidateBll = new CandidateBLL();
                    candidateBll.Save(candidate);
                    MessageBox.Show("The infomation of Candidate is saved.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                    logger.Error(ex.Message + " - " + ex.StackTrace);
                }
            }
        }

        private void LoadRole()
        {
            try
            {
                cbRole.Items.AddRange(new string[] { "Agent",
                                                "TeamLeader"
                                                   });

                cbRole.SelectedIndex = 0;
            }
            catch (Exception ex) { throw ex; }
        }

        private void LoadLevel()
        {
            try
            {
                cbLevel.Items.AddRange(new string[] { "Normal",
                                                    "Super"
                                                   });

                cbLevel.SelectedIndex = 0;
            }
            catch (Exception ex) { throw ex; }
        }

        private void LoadStatus()
        {
            try
            {
                cbStatus.Items.AddRange(new string[] { "Active",
                                                    "InActive"
                                                   });

                cbStatus.SelectedIndex = 0;
            }
            catch (Exception ex) { throw ex; }
        }

        private bool validateForm()
        {
            try
            {
                if (string.IsNullOrEmpty(txtProductCategory.Text.Trim()))
                {
                    MessageBox.Show("Product Category cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProductCategory.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtProduct.Text.Trim()))
                {
                    MessageBox.Show("Product cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProduct.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtLeaderCode.Text.Trim()))
                {
                    MessageBox.Show("Team Leader Code cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtLeaderCode.Focus();
                    return false;
                }
                else if (dtJoiningDate.IsEmpty)
                {
                    MessageBox.Show("Joining Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtJoiningDate.Focus();
                    return false;
                }
                else if (dtTransferDate.IsEmpty)
                {
                    MessageBox.Show("Promotion/Transfer Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtTransferDate.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtCategory.Text.Trim()))
                {
                    MessageBox.Show("Category cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCategory.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtFingerID.Text.Trim()))
                {
                    MessageBox.Show("Finger ID cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFingerID.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtPersonalTaxCode.Text.Trim()))
                {
                    MessageBox.Show("Personal Tax Code cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonalTaxCode.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtContactType.Text.Trim()))
                {
                    MessageBox.Show("Contract Type cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtContactType.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtContactTerm.Text.Trim()))
                {
                    MessageBox.Show("Contract Term cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtContactTerm.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtContractorName.Text.Trim()))
                {
                    MessageBox.Show("Contractor Name cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtContractorName.Focus();
                    return false;
                }
                else if (dtContractSignDate.IsEmpty)
                {
                    MessageBox.Show("Contract Sign Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtContractSignDate.Focus();
                    return false;
                }
                else if (dtContractExpiryDate.IsEmpty)
                {
                    MessageBox.Show("Contract Expiry Date cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dtContractExpiryDate.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtBankAccount.Text.Trim()))
                {
                    MessageBox.Show("Bank Account cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBankAccount.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtBankName.Text.Trim()))
                {
                    MessageBox.Show("Bank Name cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBankName.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtBankAddress.Text.Trim()))
                {
                    MessageBox.Show("Bank Address cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtBankAddress.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtFillingRefCode.Text.Trim()))
                {
                    MessageBox.Show("Filling Ref Code cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFillingRefCode.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(txtProgressTarget.Text.Trim()))
                {
                    MessageBox.Show("Progress Target cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProgressTarget.Focus();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex) { throw ex; }
        }
    }
}
